<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片 -->
      <el-card class="box-card">
        <!-- tabs -->
        <!-- v-model="activeName" 哪一个高亮 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 标签页内容 -->
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <!-- 左侧公司名 -->
              <!-- span 占多少列 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <!-- 右侧 -->
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartment('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 主体树形区域绘制 -->
            <el-tree default-expand-all :data="treeData" :props="defaultProps" class="departments-tree" @node-click="handleNodeClick">
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <!-- 添加 -->
                            <el-dropdown-item @click.native="addDepartment(data.id)">添加子部门</el-dropdown-item>
                            <!-- 编辑 -->
                            <el-dropdown-item @click.native="editDepartment(data.id)">编辑部门</el-dropdown-item>
                            <!-- 删除 -->
                            <el-dropdown-item v-if="data&&!data.children" @click.native="delDepartment(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 添加/编辑部门弹出框 -->
        <el-dialog
          :title="isEdit?'编辑部门':'添加子部门'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 弹框表单组件 -->
          <dialog-form v-if="showDialog" :parent-id="parentId" :is-edit="isEdit" :origin-list="originList" @close="updateDepartment" @cancel="showDialog=$event" />
          <!-- <span slot="footer" class="dialog-footer">
            <el-button @click="showDialog = false">取 消</el-button>
            <el-button type="primary" @click="showDialog = false">确 定</el-button>
          </span> -->
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/department'
import { transTree } from '@/utils/index'
import DialogForm from './DialogForm.vue'
export default {
  name: 'Departments',

  components: {
    DialogForm
  },

  data() {
    return {
      // tabs 高亮
      activeName: 'first',
      // 树形结构数据
      treeData: [
        // {
        //   name: '财务部',
        //   children: [
        //     {
        //       name: '财务 1 部'
        //     },
        //     {
        //       name: '财务 2 部'
        //     }
        //   ]
        // }
      ],
      // 以哪些字段 生成树形结构
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 弹出框 显隐
      showDialog: false,
      // 父级id
      parentId: '',
      // 是否是编辑
      isEdit: false,
      originList: []
    }
  },

  created() {
    // 调用获取公司组织架构数据方法
    this.getDepartments()
  },

  methods: {
    // 删除部门
    async delDepartment(id) {
      // 显示弹出框
      const delRes = await this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      // console.log(delRes)
      if (delRes === 'cancel') return
      const res = await delDepartment(id)
      // console.log(res)
      // 失败
      if (!res.success) return this.$message.error(res.message)
      // 成功 更新数据 提示
      this.getDepartments()
      this.$message.success(res.message)
    },

    // 编辑部门
    editDepartment(id) {
      // 打开弹出层
      this.showDialog = true
      // 是编辑
      this.isEdit = true
      this.parentId = id
    },

    // 更新视图
    updateDepartment() {
      // 关闭弹出框
      this.showDialog = false
      // 调用获取公司架构函数
      this.getDepartments()
    },

    // 添加部门
    addDepartment(id) {
      // 打开弹出框
      this.showDialog = true
      // 不是编辑
      this.isEdit = false
      this.parentId = id
      // console.log(id)
    },

    // 获取公司组织架构数据
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      const originList = res.data.depts.map(item => ({
        name: item.name,
        code: item.code,
        id: item.id,
        pid: item.pid
      }))
      this.originList = originList
      // console.log(originList)
      // this.data = res.data.depts
      this.treeData = transTree(res.data.depts, '')
    },

    // 点击树形节点
    handleNodeClick() {}
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
.el-tree {
  margin-top: 10px;
}
</style>
